package com.itheima.exerciseTest.method30_39.method36;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//        Class类获取构造方法对象的方法
public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        Constructor<?>[] getConstructors()      	返回所有公共构造方法对象的数组
        // method1();

//        Constructor<?>[] getDeclaredConstructors()	返回所有构造方法对象的数组
        // method2();

//        Constructor<T> getConstructor(Class<?>... parameterTypes)	返回单个公共构造方法对象
        // method3();
        // method4();

//        Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)	返回单个构造方法对象
        // method5();


    }

    private static void method5() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method36.Student");

        Constructor declaredConstructor = className.getDeclaredConstructor(String.class);

        System.out.println(declaredConstructor);

        declaredConstructor.setAccessible(true);

        Student student = (Student) declaredConstructor.newInstance("帅哥");

        System.out.println(student);
    }

    private static void method4() throws ClassNotFoundException, NoSuchMethodException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method36.Student");

        Constructor constructor = className.getConstructor(String.class,int.class);

        System.out.println(constructor);
    }

    private static void method3() throws ClassNotFoundException, NoSuchMethodException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method36.Student");

        Constructor student = className.getConstructor();

        System.out.println(student);
    }

    private static void method2() throws ClassNotFoundException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method36.Student");

        Constructor[] declaredConstructors = className.getDeclaredConstructors();

        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
    }

    private static void method1() throws ClassNotFoundException {
        Class className = Class.forName("com.itheima.exerciseTest.method30_39.method36.Student");

        Constructor[] constructors = className.getConstructors();

        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
    }
}
